/**
 * Created by shudong.msd(风骐) on 2016/4/7.
 */
"use strict";

/*从source选定dom元素加入target*/
var dynamicDom = function(options){

    //存放一些私有变量或方法
    var defaultOptions = {
        source: $(".hot-cities li span "),
        target: $(".selected-cities"),
        resultTitle :  $(".city-result-title"),
        targetItemClass : "list-group-item list-category_item selected-city",
        removeButton : $(".remove-all-cities")
    };

    var DynamicDomPlugin = function(options){
        this.source = $(options.source);
        this.target = $(options.target);
        this.resultTitle = $(options.resultTitle);
        this.removeButton = $(options.removeButton);
        this.targetItemClass = options.targetItemClass;
        this.init();
    };


    /*<label>
    <input type="checkbox" name="cities" hidden ng-model="formData['cities'][city]"><span class="category-item-value" ng-bind="city"></span>
    </label>*/

    DynamicDomPlugin.prototype = {
        selectedCount : 0,
        init:function(){
            /*if(this.source==null||this.target==null){
                throw new Error('source或target不可为空');
            }*/
            var that = this;
            if(this.source!=null){
                this.source.on('click',function(){
                    if($(this).prev().is(":checked")){
                        //移除
                        //$(this).parent().find('input:checkbox').attr('checked',null);
                        that.removeTargetLia($(this).text());

                    }else{
                        //添加
                        //$(this).parent().find('input:checkbox').attr('checked','checked');
                        that.appendTargetLia($(this).text());
                    }
                });
            }else{
                this.source = [];
            }

            if(this.removeButton!=null){
                this.removeButton.on('click',function(){
                    that.target.children().remove();
                    for(var i=0;i<that.source.length;i++){
                        if(that.source[i].prev().is(":checked")){
                            that.source[i].prev().click();
                        }
                    }
                    that.updateSelectedNo(0);
                });
            }


        },
        updateSelectedNo:function(len){
            this.selectedCount = len;
            this.resultTitle.text(len);
        },
        removeTargetLia:function(text){
            this.updateSelectedNo(this.selectedCount-1);
            for(var j = 0;j<this.target.length;j++){
                var lia = this.target.eq(j).find("li a");
                for(var i=0;i<lia.length;i++){
                    if(text==lia.eq(i).text()){
                        lia.eq(i).parent().remove();
                        break;
                    }
                }
            }

        },
        appendTargetLia:function(text){
            var selectedCity='<li class="'+this.targetItemClass+'"><a href="javascript:void(0);">'+text+'</a></li>';
            this.target.append(selectedCity);
            this.updateSelectedNo(this.selectedCount+1);
            var that = this;
            $('.'+this.targetItemClass.split(/ +/).join('.')+':last-of-type').bind('click',function(){

                that.changeCheckStateSource($(this).text());
                if(that.target.length>1){
                    that.removeTargetLia($(this).text());
                }else{
                    $(this).remove();
                    that.updateSelectedNo(that.selectedCount-1);
                }
            });
        },
        changeCheckStateSource:function(text){
            for(var i=0;i<this.source.length;i++){
                if(text==this.source[i].text()){
                    this.source[i].prev().click();
                    break;
                }
            }
        },
        isLiaExist:function(text){
            var lia = this.target.find("li a");
            for(var i=0;i<lia.length;i++){
                if(text==lia.eq(i).text()){
                    return true;
                }
            }
            return false;
        },
        removeAllSelected:function(){
            this.removeButton.click();
        },
        addEleToSource:function(ele){
            this.source.push($(ele));
            var that = this;
            $(ele).on('click',function(){
                if($(this).prev().is(":checked")){
                    //移除
                    that.removeTargetLia($(this).text());
                }else{
                    //添加
                    that.appendTargetLia($(this).text());
                }
            });
        },
        setTarget:function(ele){
            this.target = $(ele);
        },
        setResultTitle:function(ele){
            this.resultTitle = $(ele);
        },
        setRemoveButton:function(ele){
            this.removeButton = $(ele);
            var that = this;
            this.removeButton.on('click',function(){
                that.target.children().remove();
                for(var i=0;i<that.source.length;i++){
                    if(that.source[i].prev().is(":checked")){
                        that.source[i].prev().click();
                    }
                }
                that.updateSelectedNo(0);
            });
        }
    };


    var plugin = new DynamicDomPlugin(options);

    this.addToTarget = function(eleName){
        if(!plugin.isLiaExist(eleName)){
            plugin.appendTargetLia(eleName);
            //标记checked
            plugin.changeCheckStateSource(eleName);
        }

    };

    this.removeTargetDir = function(text){
        plugin.removeTargetLia(text);
        plugin.changeCheckStateSource(text);
    };

    this.removeAllSelect = function(){
        plugin.removeAllSelected();
    };

    this.addEleToSource = function(ele){
        plugin.addEleToSource(ele);
    };
    this.setTarget = function(ele){
        plugin.setTarget(ele);
    };
    this.setResultTitle = function(ele){
        plugin.setResultTitle(ele);
    };
    this.setRemoveButton = function(ele){
        plugin.setRemoveButton(ele);
    };


    return this;
};


/*
$(function(){

    $(".hot-cities li span").click(function(){
        if($(this).hasClass("selected")){
            $(this).removeClass("selected");
            var that=this;
            for(var i=0;i<$(".selected-cities").children().length;i++){
                if($(that).text()===$(".selected-cities li a").eq(i).text()){
                    $(".selected-cities li").eq(i).remove();
                    getSelectedNo();
                }
            }
        }else{
            $(this).addClass("selected");
            var selectedCity='<li class="list-group-item list-category_item selected-city"><a href="javascript:void(0);">'+$(this).text()+'</a></li>';
            $(".selected-cities").append(selectedCity);
            getSelectedNo();
            $(".selected-city").bind('click',function(){
                $(this).remove();
                for(var i=0;i<$(".selected").length;i++){
                    if($(this).text()===$(".selected").eq(i).text()){
                        $(".selected").eq(i).removeClass("selected");
                        getSelectedNo();
                    }
                }
            })
        }
    });

    $(".remove-all-cities").click(function(){
        $(".selected-cities").children().remove();
        $(".selected").removeClass("selected");
        getSelectedNo();
    });

    function getSelectedNo(){
        $(".city-result-title").text("已选择的地域("+$(".selected-cities li a").length+")");
    }

});*/
